<?php

namespace app\user\controller;

use app\model\UserQuestion as ModelUserQuestion;
use app\model\UserQuestionClass as ModelUserQuestionClass;
use app\model\UserFeedback as ModelUserFeedback;
use app\user\logic\User as UserLogicUser;

/**
 * 问题和反馈类
 * Class Common
 * @package app\user\controller
 */
class QuestionFeedback extends Member
{
    public function _initialize()
    {
        parent::_initialize();
    }

    /**
     * 问题列表 \n
     * URI : /question.list
     * @param :
     *  name          |  type  | null | description
     * ---------------|--------|------|-------------
     *  passport      | string | 必填  | 登录凭证
     *
     * @return
     *  name        |  type  | description
     * -------------|--------|-------------
     * name         | string | 类名
     * questionList | array  | 列表
     *
     * questionList
     *  name         |  type  | description
     * --------------|--------|-------------
     *  questionId   | int    | 问题id
     *  title        | string | 问题标题
     */
    public function questionList()
    {
        //类列表
        $class_list = (array)ModelUserQuestionClass::getInstance()->toSelect(
            ['is_show' => 1],
            'id,name'
        );

        //问题列表
        $question_list = (array)UserLogicUser::getInstance()->getQuestionList(
            ['class_id' => ['in', implode(',', array_column($class_list, 'id'))], 'is_show' => 1], 'id as questionId,title,class_id', 'class_id'
        );

        foreach ($class_list as &$value) {
            $value['questionList'] = isset($question_list[$value['id']]) ? $question_list[$value['id']] : [];
            unset($value['id']);
        }

        _returnData(['list' => $class_list]);
    }


    /**
     * 问题详情 \n
     * URI : /question.info
     * @param :
     *  name          |  type  | null | description
     * ---------------|--------|------|-------------
     *  passport      | string | 必填  | 登录凭证
     *  questionId    |  int   | 必填  | 问题id
     *
     * @return
     *  name        |  type  | description
     * -------------|--------|-------------
     *  info        | array  | 问题详情
     *
     * info
     *  name         |  type  | description
     * --------------|--------|-------------
     *  content      | string | 详情
     *  title        | string | 标题
     */
    public function questionInfo()
    {

        $question_id = $this->_getParams('questionId');
        $bool_res = ModelUserQuestion::getInstance()->checkQuestionId($question_id);
        _toCompare($bool_res, '==', false, 13002);
        //总列表
        $arr_info = (array)ModelUserQuestion::getInstance()->toFind(['id' => $question_id], 'title,content');

        _returnData(['info' => $arr_info]);
    }


    /**
     * 反馈 \n
     * URI : /feedback.add
     * @param :
     *  name          |  type  | null | description
     * ---------------|--------|------|-------------
     *  passport      | string | 必填  | 登录凭证
     *  content       | string | 必填  | 问题内容
     *
     * @return
     *  name        |  type  | description
     * -------------|--------|-------------
     *  ----------  |------- | 无
     */
    public function addFeedback()
    {
        $data['content'] = $this->_getContentParams('content');
        _toCompare(empty($data['content']), '==', true, 13001);

        $data['user_id'] = $this->user_id;
        $data['is_answer'] = 0;

        //添加数据
        $int_feedback_id = ModelUserFeedback::getInstance()->toAdd($data);

        if ($int_feedback_id > 0) _returnData();
        _returnError(10010);
    }
}
